import React, { Component } from 'react';
import { Text, Image, View, DeviceEventEmitter, TouchableOpacity, ScrollView, NativeAppEventEmitter } from 'react-native';
import { unitWidth, width } from '../constants/AdapterUtil';
import { MyHeader } from '../components/MyHeader';
import Colors from '../constants/Colors';
import { postFetch } from '../constants/HttpExtension';
import { show } from '../constants/Toast';
import { api_member_info_Certification, api_member_info_companyRealname, api_member_info_indeed, api_order_workorder_oneCate, api_order_workorder_sonCate } from '../constants/HttpServices';
import { SubmitButton1 } from '../components/Button';
import { MyInput1, MyInput2 } from '../components/Input';
import TextUtil from '../constants/TextUtil';
import { ChooseSingleImageItem, ChooseMultiplyImageItem, TextItem, ChooseTimeItem, ReleaseComponentItem, Item1 } from '../components/Item';
import { LoadingAndPorgressModal } from '../components/Modal';
import { uploadFile } from '../constants/UpLoadHelper';
import TimePickerUtil from '../components/TimePicker';
import AreaPicker from '../components/AreaPicker';

export default class AddAddressScreen extends Component {

    constructor(props) {
        super(props)

        const { navigation } = this.props;
        this.onSuccess = this.props.navigation.getParam('onSuccess', () => { })
        this.data = this.props.navigation.getParam('data', {})
        this.state = {
            mobile: this.data.mobile,
            name: this.data.name,
            province: this.data.province,
            city: this.data.city,
            area: this.data.area,
            address: this.data.address,
        }
    }

    componentDidMount() {

    }

    componentWillUnmount() {
        if (AreaPicker.myPicker) {
            AreaPicker.myPicker.hide();
        }
    };

    render() {
        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <MyHeader title='添加收货地址' leftImg={require('../res/ic_arrow_left.png')} leftClick={() => { this.props.navigation.pop() }}
                />
                <MyInput1
                    style={{ marginTop: 0 * unitWidth }}
                    onChangeText={(text) => { this.setState({ name: text }) }}
                    title={
                        <Text style={{ color: Colors.black, fontSize: 15 }}>收货人：</Text>
                    }
                    maxLength={11}
                    placeholder='请输入收货人姓名'
                    value={this.state.name} isLast={false} />
                <MyInput1
                    style={{ marginTop: 0 * unitWidth }}
                    onChangeText={(text) => { this.setState({ mobile: text }) }}
                    filterType={1}
                    title={
                        <Text style={{ color: Colors.black, fontSize: 15 }}>联系号码：</Text>
                    }
                    keyboardType='numeric'
                    maxLength={20}
                    placeholder='请输入联系号码'
                    value={this.state.mobile} isLast={false} />
                <Item1
                    title='所在地区：'
                    value={this.state.province ? (this.state.province + '   ' + this.state.city + '   ' + this.state.area) : ''}
                    tips='请选择所在地区'
                    onPress={() => {
                        AreaPicker.pickerInitcallback({
                            success: (province, city, area) => {
                                this.setState({
                                    province: province,
                                    city: city,
                                    area: area,
                                })
                            }
                        });
                    }} />
                <MyInput1
                    style={{ marginTop: 0 * unitWidth }}
                    onChangeText={(text) => { this.setState({ address: text }) }}
                    title={
                        <Text style={{ color: Colors.black, fontSize: 15 }}>详细地址：</Text>
                    }
                    maxLength={100}
                    placeholder='请输入详细地址'
                    value={this.state.address} isLast={true} />
                <SubmitButton1 style={{ marginTop: 48 * unitWidth, alignSelf: 'center', }} text='确定' onPress={() => {
                    if (this.state.name == '') {
                        show('请填写收货人名称');
                        return;
                    } else if (this.state.mobile == '') {
                        show('请填写联系号码');
                        return;
                    } else if (this.state.province == '') {
                        show('请选择所在地区');
                        return;
                    } else if (this.state.address == '') {
                        show('请填写详细地址');
                        return;
                    }
                    this.onSuccess ? this.onSuccess(this.state) : null
                    this.props.navigation.pop()
                }} />
            </View>
        );
    }

    //获取三级类目
    getThreeCate(id) {
        let params = {
            id: id
        }
        postFetch(api_order_workorder_sonCate, params, {
            success: (response) => {
                this.setState({
                    threeCate: response.result
                })
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}